package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.goods.activity.CommentActivity;
import com.eoner.baselibrary.bean.goods.CommodityComment;
import com.eoner.baselibrary.bean.goods.CommodityCommentContent;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.CircleTransform;

import java.util.Arrays;
import java.util.List;

public class NewCommodityDetailCommentAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;
    int width;


    // TODO 测试数据
    private static final List<String> TEST_IMAGES = Arrays.asList(
            "http://oss.gongzhugou.vip/upload_dev/review/6/6/3/6632701e46571074570c7f148bc76fbe.jpeg",
            "http://oss.gongzhugou.vip/upload_dev/review/6/6/3/6632701e46571074570c7f148bc76fbe.jpeg",
            "http://oss.gongzhugou.vip/upload_dev/review/d/8/f/d8fa881da93f95d6ae3f94314d6b916e.jpeg");

    public NewCommodityDetailCommentAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public NewCommodityDetailCommentAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        width = (int) ((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(124)) / 3f);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        TextView tv_comment_num = holder.itemView.findViewById(R.id.tv_comment_num);
        TextView tv_username = holder.itemView.findViewById(R.id.tv_username);
        TextView tv_comment = holder.itemView.findViewById(R.id.tv_comment);
        TextView tv_time = holder.itemView.findViewById(R.id.tv_time);
        ImageView iv_head = holder.itemView.findViewById(R.id.iv_head);
        View ll_comment = holder.itemView.findViewById(R.id.ll_comment);
        View tv_comment_all = holder.itemView.findViewById(R.id.tv_comment_all);
        View comment_head_layout = holder.itemView.findViewById(R.id.comment_head_layout);
//        GridLayout gl_comment_images = holder.itemView.findViewById(R.id.gl_comment_images);

        CommodityComment commodityComment = data.getComment();

        tv_comment_num.setText("用户口碑(" + commodityComment.getTotal_count() + ")");
        List<CommodityCommentContent> contents = commodityComment.getContents();
        if (contents != null && contents.size() > 0) {
            CommodityCommentContent content = contents.get(0);
            if (!TextUtils.isEmpty(content.getAvatar())) {
                App.picasso.load(content.getAvatar()).resize(ScreenUtils.dp2px(32), ScreenUtils.dp2px(32)).transform(new CircleTransform()).into(iv_head);
            }
            tv_username.setText(content.getNickname());
            tv_comment.setText(content.getDetail());
            if (!TextUtils.isEmpty(content.getShow_at())) {// && content.getShow_at().length() > 10
                tv_time.setText(content.getShow_at());
            }
        } else {
            ll_comment.setVisibility(View.GONE);
            tv_comment_num.setText("暂无评价");
            tv_comment_all.setVisibility(View.GONE);
        }

        comment_head_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (contents != null && contents.size() > 0) {
                    Intent intent = new Intent(mContext, CommentActivity.class);
                    intent.putExtra("id", data.getProduct_id());
                    mContext.startActivity(intent);
                }
            }
        });

//        if (TEST_IMAGES.size() > 0) {
//            gl_comment_images.setVisibility(View.VISIBLE);
//            gl_comment_images.removeAllViews();
//            for (int i = 0; i < TEST_IMAGES.size(); i++) {
//                String img = TEST_IMAGES.get(i);
//                ImageView imageView = new ImageView(mContext);
//                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                GlideUtil.displayRoundImage(mContext, img, imageView, 7, R.mipmap.defaults);
//                gl_comment_images.addView(imageView);
//                GridLayout.LayoutParams params = new GridLayout.LayoutParams(new ViewGroup.LayoutParams(width, width));
//                if (i % 3 != 2) {
//                    params.rightMargin = ScreenUtils.dp2px(12);
//                }
//                if (i / 3 != (TEST_IMAGES.size() - 1) / 3) {
//                    params.bottomMargin = ScreenUtils.dp2px(12);
//                }
//                imageView.setLayoutParams(params);
//                imageView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        ImageGalleryDialog imageGallery = new ImageGalleryDialog(mContext, TEST_IMAGES);
//                        imageGallery.show();
//                        imageGallery.setCurrentPage(TEST_IMAGES.indexOf(img));
//                    }
//                });
//            }
//        } else {
//            gl_comment_images.setVisibility(View.GONE);
//        }
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 4;
    }
}
